package cn.b.sky.user.controller;

import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.i18n.model.LocaleResource;
import cn.b.sky.i18n.model.condition.LocaleResourceCondition;
import cn.b.sky.i18n.service.LocaleResourceService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/** 
 * <strong>Title : LocaleResourceController.java<br></strong>
 * <strong>Package : cn.b.sky.user.controller<br></strong>
 * <strong>Description : </strong>本地化资源控制器<br> 
 * <strong>Create on : 2016-5-11 下午2:41:26<br></strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人|修改日期|修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping(value = "/localeresource/*")
@Slf4j
public class LocaleResourceController extends SecurityController {

  @Resource
  private LocaleResourceService localeResourceService;

  @RequestMapping("localeresource_list")
  public String listlocaleResource(@ModelAttribute("condition") LocaleResourceCondition condition, String navTabId,
      Model model) {
    if (condition.getResourceSet() == null) {
      condition.setResourceSet("CommonResources");
    }
    condition.setSiteCode(this.getSiteCode());
    List<LocaleResource> list = localeResourceService.selectLocaleResourceByCondition(condition);
    int count = localeResourceService.selectLocaleResourceCountByCondition(condition);
    condition.setData(list);
    condition.setTotalCount(count);
    model.addAttribute("page", condition);
    model.addAttribute(NAVTABID, navTabId);
    return "i18n/localeResource_list";
  }

  @RequestMapping("localeresource_add")
  public String addLocaleResource(Model model, String navTabId) {
    //    List<Language> data = languageService.selectLanguageNotZH();
    //    model.addAttribute("page", data);
    model.addAttribute(NAVTABID, navTabId);
    return "i18n/localeResource_add";
  }

  @RequestMapping("insertlocaleresource")
  @ResponseBody
  public DWZResult insertlocaleresource(LocaleResource localeResource, Model model, String navTabId,
                                        HttpServletRequest request) {
    localeResource.setSiteCode(this.getSiteCode());
    if (!checkLocaleResourceByKey(localeResource)) {
      return ajaxError(this.getImeViewMessageI18n(getCurrentLanguage(), "LocaleResourceRepeat.M"));
    }
    localeResourceService.insertLocaleResource(localeResource, this.getCurrentLanguage());
    return dialogAjaxDone(navTabId, null, null, null);
  }

  @RequestMapping("localeresource_modify")
  public String modifyLocaleResource(String setkey, Model model, String navTabId) {
    String[] ket = setkey.split("@_@");
    LocaleResource localeResource = localeResourceService.selectLocaleResourceByKey(ket[1], ket[0], this.getSiteCode());
    model.addAttribute("localeResource", localeResource);
    model.addAttribute(NAVTABID, navTabId);
    return "i18n/localeResource_edit";
  }

  @RequestMapping("updatelocaleresource")
  @ResponseBody
  public DWZResult saveLocaleResource(LocaleResource localeResource, Model model, String navTabId,
                                      HttpServletRequest request) {
    localeResource.setSiteCode(this.getSiteCode());
    localeResourceService.updateLocaleResource(localeResource);
    return dialogAjaxDone(navTabId, null, null, null);
  }

  @RequestMapping("localeresource_view")
  public String viewLocaleResource(String setkey, Model model, String navTabId) {
    String[] ket = setkey.split("@_@");
    LocaleResource localeResource = localeResourceService.selectLocaleResourceByKey(ket[1], ket[0], this.getSiteCode());
    //localeResource.setLanguages(languageService.selectLanguageByLocaleResourceTLKey(ket[1], ket[0]));
    model.addAttribute("localeResource", localeResource);
    model.addAttribute(NAVTABID, navTabId);
    return "i18n/localeResource_view";
  }

  @RequestMapping("localeresource_delete")
  @ResponseBody
  public DWZResult deleteLocaleResource(String resourceKeys, String navTabId, Model model, HttpServletRequest request) {
    if (StringUtils.isBlank(resourceKeys)) {
      return ajaxError(getImeViewMessageI18n(null, SystemConstant.PARAMETERNULL));
    }
    String[] idArr = resourceKeys.split(",");
    List<BatchJobReport> reports = new ArrayList<BatchJobReport>();
    int failures = 0;
    for (int i = 0; i < idArr.length; i++) {
      String id = idArr[i];
      String message = null;
      String[] setkey = id.split("@_@");
      try {
        message = localeResourceService.deleteLocaleResourceByKey(setkey[1], setkey[0], this.getSiteCode());
        message = this.getImeViewMessageI18n(this.getCurrentLanguage(), message);
      } catch (Exception e) {
        log.error("{}", e);
        message = e.getMessage();
      }
      boolean flag = true;
      if (!StringUtils.isBlank(message)) {
        failures += 1;
        flag = false;
      }
      BatchJobReport report = new BatchJobReport(idArr[i], message, flag);
      reports.add(report);
    }
    if (failures == 0) {
      return navTabAjaxDone(navTabId, null, null, null,
          getImeViewMessageI18n(this.getCurrentLanguage(), "successfulOperation.M"), null);
    } else {
      return batchjobAjaxDone(request, reports);
    }
  }
  
  /**
   * 
   * <strong>Title : flushLocalResourceCache<br></strong>
   * <strong>Description : </strong>刷新缓存，当前语言环境下的缓存，
   * 对于新增了翻译或者修改了翻译，想要及时生效，进行此操作<br> 
   * <strong>Create on : 2017-1-10 下午5:49:42<br></strong>
   * <p>
   * @return
   * DWZResult
   * @throws 
   * @author lidapeng<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping("flushLocalResourceCache")
  @ResponseBody
  public DWZResult flushLocalResourceCache() {
    localeResourceService.flushResouceCache(this.getSiteCode());
    return ajaxDone(null);
  }

  /**
   * <strong>Title : CheckLocaleResourceByKey<br></strong>
   * <strong>Description : </strong>根据key查询本地资源是否存在<br> 
   * <strong>Create on : 2016-5-16 下午2:53:27<br></strong>
   * <p>
   * @param localeResource
   * @return
   * boolean
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  private boolean checkLocaleResourceByKey(LocaleResource localeResource) {
    return localeResourceService.checkLocaleResourceByKey(localeResource) == 0;
  }
}
